package com.zlm.search.thread;

import java.util.concurrent.*;

/**
 * @Author: 郑
 */
public class ThreadTest {
    //获取线程池
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main .... start");
        /*Thread01 thread01 = new Thread01();
        thread01.start();*/
//        Runnable01 runnable01 = new Runnable01();
//        new Thread(runnable01).start();
/*        FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
        new Thread(futureTask).start();*/

        //executorService.execute(new Runnable01());
//        CompletableFuture<Void> runAsync = CompletableFuture.runAsync(() -> {
//            int i = getAnInt(12, 2, "0运行结果:");
//            System.out.println("runSync:"+i);
//        }, executorService);

        /**
         * 方法完成后的感知
         */
//        CompletableFuture<Integer> completableFuture = CompletableFuture.supplyAsync(() -> {
//            int i = getAnInt(12, 2, "1运行结果:");
//            return i;
//        },executorService).whenComplete((res,exception) -> {
//            //获取到结果和异常信息
//            System.out.println("异步方法执行完了 result:"+res+",异常:"+exception);
//        }).exceptionally(throwable -> {
//            //可以感知异常返回默认值
//            return 10;
//        });

        /**
         * 方法执行完后的处理
         */
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            int i = getAnInt(12, 2, "2运行结果:");
//            return i;
//        }, executorService).handle((res,exception) -> {
//            if(res != null){
//                return res*2;
//            }
//            if(exception != null){
//                return 0;
//            }
//            return res;
//        });

        /**
         * 线程串化
         * 1) thenRun 不能获取到上一步的执行结果
         *  .thenRunAsync(() -> {
         *             System.out.println("任务2启动");
         *             int i = getAnInt(20, 2, "2运行结果:");
         *         },executorService)
         *  2) thenAcceptAsync 和 thenAccept  区别   thenAccept 是同一个线程执行
         *      .thenAcceptAsync(res -> {
         *             System.out.println("任务2");
         *             int i = getAnInt(res, 2, "1运行结果:");
         *             System.out.println("任务2结果:" + i);
         *         })
         *  3) thenApplyAsync
         *      .thenApplyAsync(res -> {
         *             System.out.println("任务2启动");
         *             return res*2;
         *         },executorService)
         */
        CompletableFuture<Void> future1 = CompletableFuture.supplyAsync(() -> {
            int i = getAnInt(20, 2, "1运行结果:");
            return i;
        }, executorService).thenAcceptAsync(res -> {
            System.out.println("任务2");
            int i = getAnInt(res, 2, "1运行结果:");
            System.out.println("任务2结果:" + i);
        });


        System.out.println("获取到futureTask结果:" + future1.get());
        System.out.println("main... end");

    }

    private static int getAnInt(int i2, int i3, String s) {
        System.out.println("当前线程:" + Thread.currentThread().getId());
        int i = i2 / i3;
        System.out.println(s + i);
        return i;
    }

    public static class Thread01 extends Thread {
        @Override
        public void run() {
            int i = getAnInt(100, 2, "运行结果:");

        }
    }

    public static class Runnable01 implements Runnable {

        @Override
        public void run() {
            int i = getAnInt(106, 2, "运行结果:");
        }
    }

    public static class Callable01 implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {

            int i = getAnInt(10, 5, "运行结果:");
            return i;
        }
    }
}
